﻿using CoreShop.Model;
using CoreShop.Model.FromBody;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CoreShop.Library
{
    /// <summary>
    /// 说明该类型只针对类和属性有效
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
    public class CustomAttribute : Attribute
    {
        /// <summary>
        /// 构造方法初始化
        /// </summary>
        /// <param name="DisplayName"></param>
        /// <param name="DisplayType"></param>
        public CustomAttribute(string DisplayName, int DisplayType)
        {
            this.DisplayName = DisplayName;
            this.DisplayType = DisplayType;
        }
        /// <summary>
        /// 显示名称
        /// </summary>
        public string DisplayName { get; set; }
        /// <summary>
        /// 显示宽度
        /// </summary>
        public int DisplayType { get; set; }

    }


    public class ModelHelper
    {
        public static List<ComboxListDto> GetModelDisplayName<T>(T t)
        {
            Type type = typeof(T);
            PropertyInfo[] properties = type.GetProperties();
            List<ComboxListDto> list = new List<ComboxListDto>();
            foreach (var p in properties)
            {
                var item = new ComboxListDto();
                //display名字
                item.name = p.GetCustomAttribute<DisplayNameAttribute>().DisplayName;

                //对应的值
                //var value = t.GetType().GetProperty(p.Name).GetValue(t, null);
                item.value = p.Name.ToString();
                list.Add(item);
            }
            return list;
        }

        public static List<ComboxListDto> GetCustomAttribute<T>(T t)
        {
            Type type = typeof(T);
            PropertyInfo[] properties = type.GetProperties();
            List<ComboxListDto> list = new List<ComboxListDto>();
            foreach (var p in properties)
            {
                var item = new ComboxListDto();
                item.name = p.GetCustomAttribute<CustomAttribute>().DisplayName;
                item.type = p.GetCustomAttribute<CustomAttribute>().DisplayType;
                item.value = p.Name.ToString();
                list.Add(item);
            }
            return list;
        }


        public static corecmsfood PosGoodsToFood(PosGoods goods, corecmsstore store)
        {
            PosGoodSpec goodSpec = goods.pdtSpuSpecs[0];
            corecmsfood food = new corecmsfood();
            food.storeId = store.id;
            food.corpId = store.corpId;
            food.name = goods.spuName;
            food.number = goods.spuBarcode;
            food.businessType = "[1,2,3]";
            food.id = CommFunc.ConvertToBigInt(goods.spuId);
            food.foodTypeId = CommFunc.ConvertToBigInt(goods.itemSortId);
            food.foodSpecId = CommFunc.ConvertToBigInt(goodSpec.pdtSpuSpecId);
            food.type = 1;
            //food.sellStatus = goods.status.ToString();
            food.sellStatus = CommFunc.ConvertToInt32(goods.activated);
            food.specFlag = goods.pdtSpuSpecs.Count > 0;
            food.isDelete = CommFunc.ConvertIntToBool(goods.delFlag);
            food.isHeft = false;
            food.pinYin = goods.py;
            food.helpCode = goods.spuCode;
            food.isChangePrice = !CommFunc.ConvertIntToBool(goods.disItem);
            food.isDiscount = !CommFunc.ConvertIntToBool(goods.chgPrice); 
            food.addSell = CommFunc.ConvertToInt32(goods.gived);
            food.image = goods.imgUrl;
            food.remark = goods.remark;
            food.departId = goods.deptId;
            food.pointType = CommFunc.ConvertToInt32(goods.integral);
            food.byName = "";
            //if ((goods.minPrice != 0) && (goodSpec.saPrice != 0))
            //{
            //    food.minDiscount = CommFunc.ConvertToDecimal(goods.minPrice) / CommFunc.ConvertToDecimal(goodSpec.saPrice);
            //}
            return food;
        }

        public static corecmsfood GsSpuToFood(GsSpuInfoModel spuInfo, GsSpuSpecModel specModel, corecmsstore store,int parentTenantId)
        {            
            corecmsfood food = new corecmsfood();
            food.storeId = store.id;
            food.corpId = store.corpId;
            food.name = CommFunc.ConvertNullString(spuInfo.spuName);
            food.number = CommFunc.ConvertNullString(spuInfo.spuBarcode);
            food.businessType = "[1,2,3]";
            food.id = CommFunc.ConvertToBigInt(spuInfo.spuId);
            food.foodTypeId = CommFunc.ConvertToBigInt(spuInfo.itemSortId);
            food.foodSpecId = CommFunc.ConvertToBigInt(specModel.pdtSpuSpecId);
            food.type = 1;
            food.sellStatus = CommFunc.ConvertToInt32(spuInfo.status);
            if (CommFunc.ConvertToInt32(spuInfo.activated) != 1) food.sellStatus = 0;
            food.specFlag = false;
            food.isDelete = CommFunc.ConvertToBooten(spuInfo.delFlag);
            food.isHeft = false;
            food.pinYin = CommFunc.ConvertNullString(spuInfo.py);
            food.helpCode = CommFunc.ConvertNullString(spuInfo.spuCode);
            food.isChangePrice = !CommFunc.ConvertIntToBool(spuInfo.disItem);
            food.isDiscount = !CommFunc.ConvertIntToBool(spuInfo.chgPrice);
            food.addSell = CommFunc.ConvertToInt32(spuInfo.gived);
            food.image = CommFunc.ConvertNullString(spuInfo.imgUrl);
            food.remark = "";// spuInfo.remark;
            food.departId = 0;// spuInfo.deptId;
            food.pointType = CommFunc.ConvertToInt32(spuInfo.integral);
            food.byName = "";
            food.parentTenantId = parentTenantId;
            food.place = spuInfo.place;
            food.periodDay = CommFunc.ConvertToInt32(spuInfo.periodDay);
            food.posPrice = specModel.saPrice;
            food.minDiscount = 0;
            food.stand = spuInfo.stand;
            if (spuInfo.disItem) food.minDiscount = 1;
            //if ((spuInfo.minPrice != 0) && (specModel.saPrice != 0))
            //    food.minDiscount = CommFunc.ConvertToDecimal(spuInfo.minPrice) / CommFunc.ConvertToDecimal(specModel.saPrice);
            return food;
        }

        public static corecmsfoodspec GsSpuToFoodSpec(GsSpuInfoModel spuInfo, GsSpuSpecModel item, corecmsstore store)
        {
            corecmsfoodspec spec = new corecmsfoodspec();
            spec.id = item.pdtSpuSpecId;
            spec.foodId = Convert.ToInt64(item.spuId);
            spec.corpId = store.corpId;
            spec.storeId = store.id;
            spec.number = item.spuSpecCode;
            spec.code = item.spuSpecBarcode;
            spec.specName = item.spuSpecName;
            spec.name = spuInfo.spuName;
            spec.specUnitName = item.uomName;
            spec.onLine = item.useChannel.Contains("0");
            spec.isShare = true;
            spec.isDelete = CommFunc.ConvertToBooten(item.delFlag);
            spec.posPrice = CommFunc.ConvertToDecimal2(item.saPrice);//标准售价
            spec.vipPrice = CommFunc.ConvertToDecimal2(item.vipPrice);//售价2
            spec.vvipPrice = CommFunc.ConvertToDecimal2(item.vvipPrice);//售价3
            spec.commPrice = CommFunc.ConvertToDecimal2(item.commPrice);//售价4
            //是否称重(后台0称重，1不称重，注！！！！！！刚好相反)
            spec.weighType = CommFunc.ConvertToInt32(item.stand) == 0 ? 1 : 0;
            spec.weighPlu = CommFunc.ConvertToInt32(item.plu);
            spec.spuTypeId = CommFunc.ConvertToInt32(spuInfo.spuTypeId);
            spec.chgPrice = !CommFunc.ConvertToBooten(spuInfo.chgPrice);
            spec.disItem = !CommFunc.ConvertToBooten(item.disItem);
            spec.minPrice = CommFunc.ConvertToDecimal2(item.minPrice);//最低价
            spec.maxPrice = CommFunc.ConvertToDecimal2(item.maxPrice);//最高价
            return spec;
        }

        public static List<corecmsfoodspec> GoodsSpecToFoodSpec(PosGoods goods, corecmsstore store)
        {
            List<corecmsfoodspec> specList = new List<corecmsfoodspec>();
            foreach (var item in goods.pdtSpuSpecs)
            {
                corecmsfoodspec spec = new corecmsfoodspec();
                spec.id = Convert.ToInt64(item.pdtSpuSpecId);
                spec.foodId = Convert.ToInt64(item.spuId);
                spec.corpId = store.corpId;
                spec.storeId = store.id;
                spec.number = item.spuSpecCode;
                spec.code = item.spuSpecBarcode;
                spec.specName = item.spuSpecName;
                spec.name = goods.spuName;
                spec.specUnitName = item.specUomName;
                //spec.onLine = item.stopSaleFlag == 1;
                spec.onLine =CommFunc.ConvertIntToBool(goods.status);//是否上架
                spec.isShare = true;
                spec.isDelete = item.delFlag == 1;
                spec.posPrice = CommFunc.ConvertToDecimal2(item.saPrice.Value);//标准售价
                spec.vipPrice = CommFunc.ConvertToDecimal2(item.vipPrice.Value);//售价2
                spec.vvipPrice = CommFunc.ConvertToDecimal2(item.vvipPrice.Value);//售价3
                spec.commPrice = CommFunc.ConvertToDecimal2(item.commPrice.Value);//售价4
                spec.weighType = CommFunc.ConvertToInt32(item.stand);
                spec.weighPlu = CommFunc.ConvertToInt32(item.plu);
                specList.Add(spec);
            }
            return specList;
        }

        public static List<corecmsfoodcategory> GoodsCategoryToFoodCateory(List<GsSordItemClassModel> gCategoryList, corecmsstore store)
        {
            List<corecmsfoodcategory> fooCategoryList = new List<corecmsfoodcategory>();
            foreach (var item in gCategoryList)
            {
                corecmsfoodcategory category = new corecmsfoodcategory();
                category.id = Convert.ToInt64(item.itemSortId);
                category.parentId = Convert.ToInt64(item.itemSortPid);
                category.name = item.itemSortName;
                category.byName = "";
                category.appletShow = 1;
                category.helpCode = item.itemSortCode;
                category.isDelete = item.delFlag == 1;
                category.modifyTime = 0;
                category.sort = CommFunc.ConvertToInt32(item.orderNum);
                category.corpId = store.corpId;
                category.storeId = store.id;
                category.stand = CommFunc.ConvertToInt32(item.stand);
                category.tenantId = item.tenantId;
                category.canPos = item.canPos == 1;
                category.canParentPos = item.canParentPos == 1;
                fooCategoryList.Add(category);
            }
            return fooCategoryList;
        }

        public static corecmsuser VipToMember(GsPosVip vip, corecmsstore store = null)
        {
            corecmsuser member = new corecmsuser();
            member.id = Convert.ToInt64(vip.vipId);
            member.number = vip.vipNo;
            member.grade = CommFunc.ConvertToInt64(vip.vipLevelId);
            member.gradeName = vip.vipLevelName;
            member.userName = vip.vipName;
            member.mobile = vip.vipTel;
            member.sex = CommFunc.ConvertToInt32(vip.vipSix);
            member.birthday = CommFunc.ConvertToDateTime(vip.vipBirthday);
            member.starDate = CommFunc.ConvertToDateTime(vip.beginDate);
            member.endDate = CommFunc.ConvertToDateTime(vip.endDate);
            member.status = CommFunc.ConvertToInt32(vip.status);
            member.giftBalance = CommFunc.ConvertToDecimal(vip.balanceZ);
            member.sumBalance = CommFunc.ConvertToDecimal(vip.sumBalance);
            member.balance = CommFunc.ConvertToDecimal(vip.balance);
            member.storeId = store.id;
            member.corpId = store.corpId;
            member.cardNo = vip.cardNo;
            member.cardTypeId = vip.cardTypeId;
            member.cardId = CommFunc.ConvertToString(vip.cardId);
            member.point =(int)Math.Floor(vip.qty);
            member.gsPosVip = vip;

            member.discountType = (int)GlobalEnumVars.UserDiscountType.PosPrice;
            try
            {
                if (vip.vipLevelRightDto != null)
                {
                    member.VipLevelRightInfo = JsonConvert.DeserializeObject<VipLevelRightsModel>(vip.vipLevelRightDto.ToString());
                    if (member.VipLevelRightInfo != null)
                    {
                        member.discountValue = vip.vipLevelRightDto.salePriceDis * 100;
                        if (member.VipLevelRightInfo.vipPointRule != null)
                        {
                            member.VipPointRuleInfo = JsonConvert.DeserializeObject<GsVipPointRuleModel>(member.VipLevelRightInfo.vipPointRule.ToString());
                        }
                    }
                }
            }
            catch { }

            return member;
        }

        public static corecmsuser VipinfoToMember(GsVipInfo vip, corecmsstore store = null)
        {
            corecmsuser member = new corecmsuser();
            member.id = Convert.ToInt64(vip.vipId);
            member.number = vip.vipTel;
            member.gradeName = vip.levelName;
            member.userName = vip.vipName;
            member.mobile = vip.vipTel;
            member.sex = CommFunc.ConvertToInt32(vip.vipSix);
            member.birthday = Convert.ToDateTime(vip.vipBirthday);
            member.starDate = Convert.ToDateTime(vip.beginDate);
            member.endDate = Convert.ToDateTime(vip.endDate);
            member.status = CommFunc.ConvertToInt32(vip.vipStatus);
            member.balance = CommFunc.ConvertToDecimal(vip.balance);
            member.storeId = store.id;
            member.corpId = store.corpId;
            member.cardNo = vip.cardNo;
            member.cardTypeId = vip.branchId;
            member.cardId = vip.branchId;
            member.point = CommFunc.ConvertToInt32(vip.qty);
            return member;
        }


        /// <summary>
        /// 把线上的实体转成本地实体
        /// </summary>
        /// <param name="gsEmployee"></param>
        /// <param name="store"></param>
        /// <returns></returns>
        public static corecmsemployee ConvrtGsToEmployee(GsEmployeeModel item, corecmsstore store)
        {
            corecmsemployee employee = new corecmsemployee();
            employee.id = item.employeeId;
            employee.parentId = 0;
            employee.mobile = item.phoneNumber;
            employee.isDelete = CommFunc.ConvertIntToBool(item.status);
            employee.createTime = item.createTime;
            employee.name = item.employeeName;
            employee.wechatNumber = item.employeeCode;
            employee.isDefault = item.isDefault;
            return employee;
        }            
    }
}
